// import count from '@dfeidao/web/atom/nodejs/db/count';
import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
// import { IBaseTable } from '@dfeidao/atom-nodejs/db/';
// import { systemid } from '../..//atom/config';
interface Message {
	filter: {
		is_disable: boolean;
		tenant_name: string;
		tenant_level?: string;
		link_man?: object;
		productid: string
	};
	page_size: number;
	page_no: number;
	asc: string[];
	desc: string[];
}

interface Pub {
	tenant_level: string;
	level_no: string;
	level_name: string;
}


export default async function atom(msg: Message, action_id: string, session_id: string) {
	const { prepare: p1, exec: e1 } = query(action_id, session_id, '12');

	// console.log(msg, '========msg======');

	p1('pub_tenant', ['_id', 'productid', 'tenantid', 'tenant_name', 'link_man', 'phone', 'email', 'tenant_level', 'is_disable', 'create_time'], msg.filter, msg.page_size, msg.page_no, msg.asc, msg.desc);

	const [data] = await e1<Pub>();

	const tenant_levels = new Array();
	data.forEach((i) => {
		tenant_levels.push(i.tenant_level);
	});

	p1('tenant_level', ['_id', 'productid', 'level_no', 'level_name', 'order_num', 'create_time'], { level_no: { $in: tenant_levels } }, 100, 1, [], []);
	const [data1] = await e1<Pub>();
	data.forEach((i) => {
		data1.forEach((j) => {
			if (i.tenant_level === j.level_no) {
				i.level_name = j.level_name;
			}
		});
	});

	const c = count(action_id, session_id, '12');
	c.prepare('pub_tenant', msg.filter);
	const [cnt] = await c.exec();
	return { count: cnt, dataArray: data, page_no: msg.page_no, page_size: msg.page_size };
}
